from strategy_mode import str_cal
import re
import math


def Strategy_Main(
    df,
    pre="5",
    GroupOfLines=3,
    printLens=60,
    Ma_Day_List=[],
    keyls=["close", "low", "high", "open", "Avg"],
):
    col_Les = df.columns.tolist()
    print(str(Ma_Day_List).center(printLens, " "))

    df = str_cal.MAsBuliderMAIN(df, keyls, Ma_Day_List, shift=False)  # 计算各种MA，MA布尔值等等
    df = str_cal.Macount(df, Ma_Day_List)
    df = str_cal.Ma_order_count(df, Ma_Day_List, keyls)
    for num in Ma_Day_List:
        col_Les.append("Avg_ma_{}".format(num))

    # )  # 计算卖出信号，红蓝占比

    df = str_cal.BollFastSum_All(
        df, keyls, "_by%", sub_Key="", Ma_Day_List=Ma_Day_List, drop=False
    )

    df = df.fillna(0)
    pre_g = re.findall("\d*", pre)
    if pre_g != ["", ""]:
        gap = 240 / int(pre)
    else:
        if "D" in pre.upper():
            gap = 8
        if "M" in pre.upper() or "Y" in pre.upper():
            gap = 1
    fillin = int(gap * 4)
    gap = int(gap)
    gap2 = (1 - math.log(gap) * 0.1) * 1.2
    cat = "All_by%_sum_{}_rolling".format(fillin)
    df.loc[:, "T_" + cat] = df["All_by%_sum"].rolling(fillin).mean()
    df.loc[:, "TH_" + cat.format(fillin)] = (
        df["T_" + cat] + df["All_by%_sum"].rolling(fillin).std() * gap2
    )
    df.loc[:, "TL_" + cat.format(fillin)] = (
        df["T_" + cat] - df["All_by%_sum"].rolling(fillin).std() * gap2
    )

    cat = "All_sort_{}_rolling".format(int(fillin))
    df.loc[:, "T_" + cat] = df["All_sort"].rolling(fillin).mean()
    sorter = 0
    for col in df.columns.tolist():
        if "All" in col:
            if sorter < 9:
                col_Les.append(col)
                sorter += 1

    df = df[col_Les]
    return df